<template>
	<view>
		<view v-if="isLogin">
			<view class="head-container">
				<image :src="avatar" mode="aspectFill" class="img-avatar"></image>
				<view class="user-info-container">
					<view class="nick-type-container">
						<text class="nick-badge">{{nick + "[" + userTypes[type]+"]"}}</text>
						<image :src="badges[type-1]" mode="widthFix" class="img-badge"></image>
					</view>
					<text class="user-email">{{name + ':' + email}}</text>
				</view>
			</view>
			<!-- 注销 -->
			<text class="iconfont icon-zhuxiao icon-zhuxiao" @click="clickLogout"></text>
		</view>
		<login-dialog ref="login" @after="afterLogin"></login-dialog>
	</view>
</template>

<script>
	const app = getApp()
	export default {
		data() {
			return {

				isLogin: app.globalData.token.length > 0,
				avatar: this.$params.host + app.globalData.avatar,
				type: app.globalData.type,
				user: "",
				nick: "",
				email: "",
				userTypes: ["管理员", "教师", "学生"],
			}
		},
		mounted() {
			if (!this.isLogin) {
				this.$refs.login.show()
			}
		},
		onLoad() {
			this.$noti.add(this.$params.noti_login_status, this, this.afterLogin, )
		},
		onUnload() {

			this.$noti.remove(this.$params.noti_login_status, this)
		},
		methods: {

			afterLogin(logout) {
				if (logout) {
					this.$refs.login.show()
					return
				}
				this.isLogin = app.globalData.token.length > 0
				this.avatar = this.$params.host + app.globalData.avatar
				this.type = app.globalData.type
				this.user = app.globalData.userName
				this.email = app.globalData.email
				this.nick = app.globalData.nickname
			},
			//注销
			clickLogout() {
				this.isLogin = false
				app.globalData.avatar = '../../static/icons/default_user.png'
				app.globalData.uid = ''
				app.globalData.type = -1
				app.globalData.email = ''
				app.globalData.nickName = ''
				app.globalData.userName = ''
				app.globalData.token = ''
				this.$noti.post(this.$params.noti_login_status, true)
			},
			clickSwitchPanel(index) {
				let now = this.panelStatus[index].visible
				this.panelStatus[index].visible = !now
			}
		}
	}
</script>

<style>
	@import url("profile.css");
</style>
